package com.lwy.controller;


import com.lwy.domain.Person;
import com.lwy.domain.Student;
import com.lwy.feign.GatewayFeignClient;

import com.lwy.redis.RedissonUtil;
import com.lwy.service.AdminService;
import com.lwy.service.impl.TrainClassFactory;
import com.lwy.setter.NameSetters;
import com.lwy.setter.dto.BaseCarrierDTO;
import com.lwy.setter.enums.TypeEnum;
import lombok.extern.slf4j.Slf4j;

import org.redisson.api.RMap;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RequestMapping("/admin")
@RestController
@Slf4j
public class LwyAdminController {

    @Autowired
    private TrainClassFactory trainClassFactory;

    @Autowired
    private Environment environment;

    @Autowired
    private GatewayFeignClient gatewayFeignClient;

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RedissonUtil redissonUtil;


    //策略模式
    @GetMapping("/getStudent")
    public Student getStudent(@RequestParam String beanName) {
        return trainClassFactory.handle(beanName);
    }


    //获取配置
    @GetMapping("/getStudentConfig")
    public Student getStudentConfig(@RequestParam Integer type) {
        // one
        Student student = new Student();
        student.setId(environment.getProperty("student.id"));
        student.setName(environment.getProperty("student.name"));
        student.setAge(Integer.valueOf(environment.getProperty("student.age","")));
        return student;
    }


    //spring cloud feign  nacos 从。4.0需要下载这个
    @GetMapping("/getStudentById")
    public Student getStudentById(@RequestParam String id) {
        return gatewayFeignClient.getStudentById(id);
    }

    //mybatis
    @GetMapping("/getPerson")
    public Person getPerson(){
        return adminService.getPerson();
    }

    // Redisson框架
    @GetMapping("/getRedisDetail")
    public String getRedisDetail(@RequestParam String vehicleId){
        String str = "";
        String redisKey = "lwy_BBB";
        List<String> list = Arrays.asList("AAA","BBB","CCC");
        Collections.shuffle(list);
        boolean exists = redissonClient.getBucket(redisKey).isExists();
        RMap<String, Integer> rMap = redissonClient.getMap(redisKey);
        if (!exists){
            for (String vehicleCode : list) {
                rMap.put(vehicleCode,1);
                str = vehicleCode;
                break;
            }
        }else {
            int maxCount = getMaxCountA(rMap);
            for (String vehicleCode : list) {
                Integer vehicleCount = Objects.isNull(rMap.get(vehicleCode)) ? 0 :  rMap.get(vehicleCode);
                if (vehicleCount < maxCount){
                    rMap.remove(vehicleCode);
                    rMap.put(vehicleCode,vehicleCount + 1);
                    str = vehicleCode;
                    break;
                }
            }
            if (StringUtils.isEmpty(str)){
                str  = list.get(0);
                Integer count = Objects.isNull(rMap.get(str)) ? 0 : rMap.get(str);
                rMap.remove(str);
                rMap.put(str, count + 1);
            }
        }
        for (Map.Entry<String, Integer> map : rMap.entrySet()) {
            System.out.println(map.getKey() + " : " + map.getValue());
        }
        System.out.println("------------");
       return str;
    }

    private int getMaxCountA(RMap<String, Integer> rMap) {
        int size = rMap.size();
        Collection<Integer> values = rMap.values();
        Object[] array = values.toArray();
        Arrays.sort(array);
        return (int)array[size - 1];
    }


    // Redisson框架
    @GetMapping("/getRedisZSet")
    public String getRedisZSet(@RequestParam Integer count){
        String redisKey = "sdfljsfsfgdffd";
        String key = "BBB";
        RScoredSortedSet<String> scoredSortedSet = redissonClient.getScoredSortedSet(redisKey, StringCodec.INSTANCE);
        scoredSortedSet.addScore(key, 100);
        Double score = scoredSortedSet.getScore(key);
        String str = score.toString();
        return str;
    }

    //测试下自定义注解 1
    @GetMapping("/getAnnotation")
    public BaseCarrierDTO getAnnotation(@RequestParam Integer id){
        BaseCarrierDTO baseCarrierDTO = new BaseCarrierDTO();
        baseCarrierDTO.setCarrierCode("code");
        baseCarrierDTO.setType(TypeEnum.OWNED.name());
        baseCarrierDTO.setTypeValue(TypeEnum.OWNED.getValue());
        return NameSetters.fillNames(baseCarrierDTO);
    }
}
